*** Set paths
local dir "..."
local dropbox_data "..."
local dropbox_data_create "..."
local local_data "..."
local dropbox_fig "..."
local dropbox_tab "..."

***************************************************************************
**** MODEL W/O HOMEDEALERS
***************************************************************************
	
*initialize variables
gen share_Ei 		  	  =.
gen Ei_N 		  	  	  =.
gen share_OTCi	          =.
gen OTCi_N          	  =.

gen side = (IIROC_SIDE=="BUY")

*fill in information looping over these variables and each side
levelsof side, local(levels_sides) 
foreach l of local levels_sides {

	*E-market share: shareE_tsi in number of trades
	bys bidderid  period IIROC_SIDE trading_venue large_trade: gen a0 = _N if trading_venue=="CanDeal" & side==`l'
	bys bidderid  period  IIROC_SIDE large_trade: egen a1 = max(a0) // copy across trading_venues
	replace Ei_N=a1 if  side==`l'
	replace Ei_N = 0 if Ei_N==. & market_type=="secondary" & side==`l'
	drop a0 a1

	*shareE_tsi as % of total platform market share (across dealers)
	bys period IIROC_SIDE trading_venue large_trade: gen a0 = _N if trading_venue=="CanDeal" & side==`l'
	bys period IIROC_SIDE large_trade: egen a1 = max(a0) // copy across trading_venues
	replace a1 = 0 if a1==. & market_type=="secondary" & side==`l'
	replace share_Ei = Ei_N/a1 if side==`l'
	drop a0 a1

	*OTC market share: shareOTC_tsi
	bys bidderid  period IIROC_SIDE trading_venue large_trade: gen a0 = _N if trading_venue=="OTC" & side==`l'
	bys bidderid  period IIROC_SIDE large_trade: egen a1 = max(a0) // copy across trading_venues
	replace OTCi_N=a1 if  side==`l'
	replace OTCi_N = 0 if OTCi_N==. & market_type=="secondary" & side==`l'
	drop a0 a1 

	*shareOTC_tsi as % of total  market share of that dealer (OTC and bilateral)
	bys bidderid period IIROC_SIDE large_trade: gen a1 = _N if  side==`l'
	replace a1 = 0 if a1==. & market_type=="secondary" & side==`l'
	replace share_OTCi = OTCi_N/a1 if side==`l'
	drop a1
}

*check
count if share_Ei==.


***************************************************************************
**** HOMEDEALER MODEL
***************************************************************************
	
*initialize variables
gen share_Ei_H 		  	  =.
gen Ei_N_H 		  	  	  =.
gen share_OTCi_H	      =.
gen OTCi_N_H          	  =.

*fill in information looping over these variables and each side
levelsof side, local(levels_sides) 
foreach l of local levels_sides {

	*E-market share: shareE_tsi in number of trades
	bys bidderid homedealerID period IIROC_SIDE trading_venue large_trade: gen a0 = _N if trading_venue=="CanDeal" & side==`l' 
	bys bidderid homedealerID period IIROC_SIDE large_trade: egen a1 = max(a0) // copy across trading_venues			
	replace Ei_N_H=a1 if  side==`l' 																					    
	replace Ei_N_H = 0 if Ei_N_H==.  & market_type=="secondary" & side==`l'  												
	drop a0 a1

	*shareE_tsi as % of total platform market share (across dealers)
	bys homedealerID period IIROC_SIDE trading_venue large_trade: gen a0 = _N if trading_venue=="CanDeal" & side==`l'  
	bys homedealerID period IIROC_SIDE large_trade: egen a1 = max(a0)  // copy across trading_venues
	replace a1 = 0 if a1==. & market_type=="secondary" & side==`l'  									
	replace share_Ei_H = Ei_N_H/a1 if side==`l'  														
	drop a0 a1
}

*check
count if share_Ei_H==. 
